import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('day.csv')
pd.set_option('display.max_columns',None)
pd.set_option('display.max_rows',None)
print(df.head())
#离散特征
categorical_features = ['season','mnth','weathersit','weekday']
for col in categorical_features:
    print('\n%s属性的不同取值和出现次数'%col)
    df[col] = df[col].astype('object')
    print(df[col].value_counts())


num_features = ['temp','atemp','hum','windspeed']
df[num_features].hist();plt.show()
sns.violinplot(x='yr',y='cnt',data=df[['yr','cnt']]);plt.show()
_,ax = plt.subplots()
sns.barplot(data=df[['season','cnt']],x='season',y='cnt')
ax.set(title='seasonly distribution of counts');plt.show()
sns.barplot(data=df[['mnth','cnt']],x='mnth',y='cnt')
ax.set(title='monthly distribution of counts');plt.show()
_,axs = plt.subplots(ncols=2)
sns.barplot(data=df,x='holiday',y='cnt',ax=axs[0])
sns.barplot(data=df,x='workingday',y='cnt',ax=axs[1])
plt.show()

corr = df[['temp','atemp','hum','windspeed','casual','registered','cnt']].corr()
data_corr = corr.abs()
plt.subplots(figsize = (10,6))
sns.heatmap(data_corr,annot=True)
sns.heatmap(data_corr,mask = data_corr < 0.5,annot=True)
plt.axis('tight')
plt.show()

y = df['cnt']
log_y = np.log1p(y)

#独热编码
categorical_features = ['season','mnth','weathersit','weekday','yr','holiday']
for col in categorical_features:
    df[col] = df[col].astype('object')
X_train_cat = df[categorical_features]
X_train_cat = pd.get_dummies(X_train_cat)
print(X_train_cat.head())


#标准化去量纲
from sklearn.preprocessing import MinMaxScaler
mn_X = MinMaxScaler()
num_features = ['temp','atemp','hum','windspeed']
X = mn_X.fit_transform(df[num_features])
fe_data = pd.DataFrame(data=X,columns=num_features,index=df.index)
fe_data = pd.concat([fe_data,X_train_cat],axis=1,ignore_index=False)
fe_data['cnt'] = y
fe_data['log_cnt'] = log_y
fe_data.to_csv('FE_day.csv',index=False)
print(fe_data.info(),fe_data.head())



from math import sqrt
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
df1 = pd.read_csv('FE_day.csv')
y1 = df1['log_cnt']
X1 = df1.drop('cnt',axis=1)
feat_names = X1.columns

X_train,X_test,y_train,y_test = train_test_split(X1,y1,random_state=42,test_size=0.2)
'最小二乘'
lr = LinearRegression()
lr.fit(X_train,y_train)
y_test_pred_lr = lr.predict(X_test)
y_train_pred_lr = lr.predict(X_train)
fs = pd.DataFrame({'columns':list(feat_names),'coef':list((lr.coef_.T))})
print(fs.sort_values(by=['coef'],ascending=False))
print('The rooted mean_squared_error score of LinearRegression on test is',sqrt(mean_squared_error(y_test,y_test_pred_lr)))
print('The rooted mean_squared_error score of LinearRegression on train is',sqrt(mean_squared_error(y_train,y_train_pred_lr)))


'岭回归'
from sklearn.linear_model import RidgeCV
alphas = [0.0001,0.001,0.01,0.1,1,10,100,1000]
ridge = RidgeCV(alphas=alphas,store_cv_values=True)
ridge.fit(X_train,y_train)
y_test_pred_rigde = ridge.predict(X_test)
y_train_pred_rigde = ridge.predict(X_train)
fs = pd.DataFrame({'columns':list(feat_names),'coef_lr':list((lr.coef_.T)),'coef_ridge':list((ridge.coef_.T))})
print(fs.sort_values(by=['coef_lr'],ascending=False))
print('The  rooted mean_squared_error score of RigdeCV on test is',sqrt(mean_squared_error(y_test,y_test_pred_rigde)))
print('The  rooted mean_squared_error score of RigdeCV on train is',sqrt(mean_squared_error(y_train,y_train_pred_rigde)))
RMSE_mean = np.mean(ridge.cv_values_,axis=0)
plt.plot(np.log10(alphas),RMSE_mean.reshape(len(alphas),1))
plt.xlabel('log(alpha)')
plt.ylabel('RMSE')
print('alpha is:',ridge.alpha_)
plt.show()


'lasso'
from sklearn.linear_model import LassoCV
lasso = LassoCV()
lasso.fit(X_train,y_train)
y_test_pred_lasso = ridge.predict(X_test)
y_train_pred_lasso = ridge.predict(X_train)
fs = pd.DataFrame({'columns':list(feat_names),'coef_lr':list((lr.coef_.T)),'coef_ridge':list((ridge.coef_.T)),'coef_lasso':list((lasso.coef_.T))})

print(fs.sort_values(by=['coef_lr'],ascending=False))
print('The rooted mean_squared_error score of lassoCV on test is',sqrt(mean_squared_error(y_test,y_test_pred_lasso)))
print('The rooted mean_squared_error score of lassoCV on train is',sqrt(mean_squared_error(y_train,y_train_pred_lasso)))

RMSE_means = np.mean(lasso.mse_path_,axis=1)#mse_path_: 每次交叉验证的均方误差
plt.plot(np.log10(lasso.alphas_),RMSE_means)
plt.xlabel('log(alpha)')
plt.ylabel('RMSE')
print('alpha is:',lasso.alpha_)
plt.show()